﻿// 注意方法名的格式为 valid+模块名
const validModule = {
  validProductModule (component) {
    if (!component.uploadTag) {
      return { flag: false, message: '商品组件必须上传商品！' }
    }
    return { flag: true }
  },
  validElevatorModule (component) {
    for (let i = 0; i < component.elevatorList.length; i++) {
      let elevator = component.elevatorList[i]
      if (!elevator.uploadExcelName) {
        return {
          flag: false,
          message: '电梯楼层必须上传商品！楼层：' + elevator.name,
        }
      }
    }
    return { flag: true }
  },
  validSlideModule (component) {
    const valids = component.list.filter(item => {
      return item.imageUrl !== '' && !item.defalut
    })
    if (valids && valids.length > 0) {
      return { flag: true }
    } else {
      return { flag: false, message: '未配置轮播图' }
    }
  },
  validImageModule (component) {
    for (let i = 0; i < component.imageList.length; i++) {
      let item = component.imageList[i]
      if (!item.image) {
        return {
          flag: false,
          message: component.name + '组件必须上传图片！',
        }
      }
    }

    if (
      component.layout === 4 &&
      component.imageList.length < component.showPicNum
    ) {
      return {
        flag: false,
        message:
          component.name + '组件至少上传' + component.showPicNum + '张图片！',
      }
    }
    return { flag: true }
  },

  validCouponModule (component,componentList,bizData) {
    let biz = bizData[component._id]
    if (!biz || biz.length===0){
      return { flag: false, message: '未配置优惠券' }
    }
    let list = biz
    if (list[0].id === -1) {
      return { flag: false, message: '未配置优惠券' }
    }
    if (component.capitalAccount == '' || component.capitalAccount == null) {
      let dsList = ['K1001', 'K1001_V', 'K1002', 'K1003', '1005']
      let node = list.find(a => dsList.includes(a.couponType))
      if (node) {
        return {
          flag: false,
          message: '导入的券中包含了电商券，必须填写资金账号！',
        }
      }
    }

    if (
      component.immediate === 2 &&
      !component.showAllConfigs &&
      component.allLink === ''
    ) {
      return { flag: false, message: '未配置立即领取链接' }
    }

    if (component.showAllConfigs) {
      let valids = []
      if (component.showAllConfigs) {
        valids = list.filter(item => {
          return item.linkUrl && item.linkUrl !== ''
        })
      }

      if (valids && valids.length === list.length) {
        return { flag: true }
      } else {
        return { flag: false, message: '未配置立即领取链接' }
      }
    }
    if (component.limitTag) {
      if (component.limitArray && component.limitArray.length > 0) {
        for (let i = 0; i < component.limitArray.length; i++) {
          if (!component.limitArray[i].num) {
            return {
              flag: false,
              message:
                component.name + '领取限制条件填写无效！必须填写大于0的数字！',
            }
          }
        }
      } else {
        return {
          flag: false,
          message:
            component.name + '勾选了领取限制，领取条件必须填写其中一条！',
        }
      }
    }

    return { flag: true }
  },
  validMagicCubeModule (component) {
    for (let i = 0; i < component.imageList.length; i++) {
      let item = component.imageList[i]
      if (!item.image) {
        return {
          flag: false,
          message: component.name + '组件必须上传图片！',
        }
      }
    }
    return { flag: true }
  },
  validNavigateModule (component) {
    for (let i = 0; i < component.imageList.length; i++) {
      let item = component.imageList[i]
      if (!item.image) {
        return {
          flag: false,
          message: component.name + `组件中，第${i + 1}个导航未上传图片！`,
        }
      }
    }

    return { flag: true }
  },
  validProductRightsModule (component) {
    if (
      !component.tagOf1 &&
      !component.tagOf2 &&
      !component.tagOf3 &&
      !component.tagOf4
    ) {
      return {
        flag: false,
        message: component.name + '请至少设置一个相关人群~！',
      }
    } else if (
      !component.checkedOf1 &&
      !component.checkedOf2 &&
      !component.checkedOf3 &&
      !component.checkedOf4
    ) {
      return {
        flag: false,
        message: component.name + '请至少勾选一个人****类型',
      }
    } else if (
      (component.checkedOf1 && !component.tagOf1) ||
      (component.checkedOf2 && !component.tagOf2) ||
      (component.checkedOf3 && !component.tagOf3) ||
      (component.checkedOf4 && !component.tagOf4)
    ) {
      return {
        flag: false,
        message:
          component.name + '请为您所有勾选的******类型添加对应的人群包',
      }
    } else {
      if (
        component.tagOf1 &&
        (component.titleName1 === '' ||
          component.btnText1 === '' ||
          component.jumpUrl1 === '')
      ) {
        return {
          flag: false,
          message: component.name + '--***设置项目有空余项,请补充完整',
        }
      }
      if (
        component.tagOf2 &&
        (component.titleName2 === '' ||
          component.btnText2 === '' ||
          component.jumpUrl2 === '')
      ) {
        return {
          flag: false,
          message: component.name + '--*****设置项目有空余项,请补充完整',
        }
      }
      if (
        component.tagOf3 &&
        (component.titleName3 === '' ||
          component.btnText3 === '' ||
          component.jumpUrl3 === '')
      ) {
        return {
          flag: false,
          message: component.name + '--*****设置项目有空余项,请补充完整',
        }
      }
      if (
        component.tagOf4 &&
        (component.titleName4 === '' ||
          component.btnText4 === '' ||
          component.jumpUrl4 === '')
      ) {
        return {
          flag: false,
          message: component.name + '--****设置项目有空余项,请补充完整',
        }
      }
    }
    return { flag: true }
  },
  validBeatMouseModule (component) {
    if (!component.gameNo) {
      return {
        flag: false,
        message: component.name + '请关联相关游戏ID',
      }
    } else {
      return { flag: true }
    }
  },
  validTurnOverModule (component) {
    if (!component.hasCoupon) {
      return {
        flag: false,
        message: component.name + '请添加券！',
      }
    } 
    
    if (component.limitTag && (!component.limitArray  || component.limitArray.length === 0) ) {
      return {
        flag: false,
        message: component.name + '领取限制条件至少填一条！',
      }
    }

    return { flag: true }
     
  },
  validQuotaModule (component, componentList, bizData) {
    console.log(666,component.hasCoupon, componentList, bizData)
    if (!component.hasCoupon) {
      return {
        flag: false,
        message: component.name + '请添加券！',
      }
    } else {
      return { flag: true }
    }
  },
  validCouponPackageModule (component,componentList,bizData) {
    if (component.packageFields.includes(2) && !component.expirationTime) {
      return {
        flag: false,
        message: component.name + '勾选了倒计时，有效期必须填写！！',
      }
    }

    let biz = bizData[component._id]
    if (biz){
      let realList = biz.filter(a => !a.test_data)
      if (!realList || realList.length === 0) {
        return {
          flag: false,
          message: component.name + '至少添加一个qb！',
        }
      }

      if (realList.length < 2 && component.layout === 2) {
        return {
          flag: false,
          message: component.name + '该样式下至少配置两个qb！',
        }
      }

    } else {
      return {
        flag: false,
        message: component.name + '至少添加一个qb！',
      }
    }
    
    if (component.capitalAccount === '' || component.capitalAccount === null) {
      let dsList = ['K1001', 'K1001_V', 'K1002', 'K1003', '1005']
      let node = biz.data.find(a => dsList.includes(a.couponType))
      if (node) {
        return {
          flag: false,
          message: '必须填写资金账号！',
        }
      }
    }

    if (component.limitTag) {
      if (component.limitArray && component.limitArray.length > 0) {
        for (let i = 0; i < component.limitArray.length; i++) {
          if (!component.limitArray[i].num) {
            return {
              flag: false,
              message:
                component.name + '领取限制条件填写无效！必须填写大于0的数字！',
            }
          }
        }
      } else {
        return {
          flag: false,
          message:
            component.name + '勾选了领取限制，领取条件必须填写其中一条！',
        }
      }
    }
    if (component.fieldType && component.fieldType === 2) {
      console.log(biz)
      let packageSubtitle = biz.find(a => !a.packageSubtitle)
      let packageDenomination = biz.find(
        a => !a.packageDenomination
      )
      if (packageSubtitle) {
        return {
          flag: false,
          message:
            component.name +
            '，字段取值类型为券包，添加的券包数据中，副标题不能为空，请检查配置！！',
        }
      }
      if (packageDenomination) {
        return {
          flag: false,
          message:
            component.name +
            '，字段取值类型为券包，添加的券包数据中，面额不能为空，请检查配置！！',
        }
      }
    }
    if (component.layout === 3) {

      let unReceiveImage = component.imageGroupList.findIndex(a => !a.unReceiveImage.image)
      let receivedImage = component.imageGroupList.findIndex(a => !a.receivedImage.image)
      let receiveOutImage = component.imageGroupList.findIndex(a => !a.receiveOutImage.image)
      if (unReceiveImage >= 0) {
        return {
          flag: false,
          message: component.name + `，第${unReceiveImage + 1}组图片中，未领取图片必须上传！`,
        }
      }
      if (receivedImage >= 0) {
        return {
          flag: false,
          message: component.name + `，第${receivedImage + 1}组图片中，已领取图片必须上传！`,
        }
      }
      if (receiveOutImage >= 0) {
        return {
          flag: false,
          message: component.name + `，第${receiveOutImage + 1}组图片中，已抢光图片必须上传！`,
        }
      }

    }
    return { flag: true }
  },
  validSecKillModule (component,componentList,bizData) {
  
    let node = bizData[component._id]
    if (!node ||  node.length===0){
      return {
        flag: false,
        message: component.name + '组件中，还没有上传优惠券！',
      }
    }

    let validList = node.filter(a => !a.test_data)
    if (!validList || validList.length === 0) {
      return {
        flag: false,
        message: component.name + '组件中，还没有上传优惠券！',
      }
    }
    if (validList.length < 2 || validList.length > 3) {
      return {
        flag: false,
        message: component.name + '组件中，只能添加2-3个优惠券！',
      }
    }

    if (!component.startDate || !component.endDate) {
      return {
        flag: false,
        message: component.name + '组件中，****周期必填！',
      }
    }

    if (new Date(component.endDate) <= new Date(component.startDate)) {
      return {
        flag: false,
        message: component.name + '周期开始时间必须小于结束时间！',
      }
    }

    if (!component.beginTime) {
      return {
        flag: false,
        message: component.name + '组件中，每日开抢时间必填！',
      }
    }
    if (component.receivedText == 2 && !component.allLink) {
      return {
        flag: false,
        message: component.name + '组件中，领取后显示立即使用，跳转链接必填！',
      }
    }
    return { flag: true }
  },
  validAnchorPoint ({ tabs, name, iconType }, list) {
    if (tabs.length < 2) return { flag: false, message: name + '请至少添加两个菜单！' }
    let title = '', menuNo = 0

    const withoutName = tabs.some((item, index) => { !item.name && (menuNo = index); return !item.name })
    if (withoutName) return { flag: false, message: name + `菜单${menuNo + 1}必须有名称！` }

    const withoutLabelComponent = tabs.some(item => {
      !item.labelComponentId && (title = item.name)
      return !item.labelComponentId
    })
    if (withoutLabelComponent) return { flag: false, message: name + `菜单${title}必须关联组件！` }

    const withoutIcon = tabs.some((item) => {
      (!item.iconUrl || !item.activeIconUrl) && (title = item.name)
      return !item.iconUrl || !item.activeIconUrl
    })

    if (withoutIcon && iconType === 1) return { flag: false, message: name + `菜单${title}必须配置图标！` }

    if (list.filter(item => item.properties === 'AnchorPointEditor').length > 1) {
      return { flag: false, message: '锚点组件最多只能有一个!' }
    }

    const hasAnchorPoint = list.filter(item => item.properties === 'AnchorPointEditor').length
    const hasElevatorModule = list.filter(item => item.properties === 'ElevatorEditor').length
    if (hasAnchorPoint && hasElevatorModule) return { flag: false, message: '锚点组件和电梯组件不能同时存在!' }

    let mapIndex = [], noLabelComponent = []
    tabs.forEach(item => {
      if (!list.some(component => component._id === item.labelComponentId)) {
        noLabelComponent.push(item)
      }
      list.forEach((component, index) => {
        if (component._id === item.labelComponentId) {
          mapIndex.push(index)
        }
      })
    })
    if (noLabelComponent.length) {
      return { flag: false, message: `${noLabelComponent[0].name}关联的组件被删除!` }
    }
    if (String(mapIndex) !== String(mapIndex.sort((a, b) => a - b))) {
      return { flag: false, message: '锚点组件关联的组件顺序必须与锚点组件内一致!' }
    }

    return { flag: true }
  },
  validIncreaseLimitModule (component,componentList,bizData) {
   
    if (!component.connectId ) {
      return {
        flag: false,
        message: component.name + '组件中，未关联提额ID！',
      }
    } 
    return { flag: true }
  },
}
export default validModule
